//
//  RHUserGroupCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2024/11/13.
//

#import "RHUserGroupCell.h"
#import "RHTools.h"

@interface RHUserGroupCell()
@property (nonatomic ,strong) QMUILabel *plantGroupName;

@property (nonatomic ,strong) QMUILabel *totalUserLab;//用户数量

@property (nonatomic ,strong) QMUILabel *groupTypeLab;//分组类型

@property (nonatomic ,strong) QMUILabel *descriteLab;
@end
@implementation RHUserGroupCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHUserGroupModel *)model
{
    _model = model;
    self.plantGroupName.text = model.groupName;
    self.totalUserLab.text = [NSString stringWithFormat:@"%ld",model.userNum];
    self.groupTypeLab.text = ValidNullDataToString(model.type, @"--");
    self.descriteLab.text = ValidNullDataToString(model.desc, @"--");;

}

- (void)setupUI{

    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    kWeakSelf;
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView.layer setCornerRadius:8];
    [self.contentView addSubview:bgView];
    
    _plantGroupName = [[QMUILabel alloc] init];
    [_plantGroupName setFont:[UIFont boldSystemFontOfSize:16]];
    _plantGroupName.adjustsFontSizeToFitWidth = YES;
    _plantGroupName.minimumScaleFactor = 0.8;
    [_plantGroupName setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_plantGroupName];
    
  
    
    QMUIButton *moreBtn = [[QMUIButton alloc]init];
    [moreBtn setImage:[UIImage imageNamed:@"vpp_set_more"] forState:UIControlStateNormal];
    [bgView addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.right.mas_offset(35);
        make.right.mas_offset(-15);
        make.centerY.equalTo(weakSelf.plantGroupName);
    }];
    
    [[moreBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.moreBlock) {
           CGRect rect = [weakSelf.contentView convertRect:moreBtn.frame toView:kAppWindow];
            weakSelf.moreBlock(weakSelf.model,rect);
        }
    }];
    
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
    [bgView addSubview:line];
    
    UIView *bgView2 = kView;
    bgView2.backgroundColor = CFTabViewBgColor;
    [bgView2 rounded:8];
    [bgView addSubview:bgView2];
    
    UIView *line_vertical = kView;
    line_vertical.backgroundColor = kCOLOR_HEX_A(@"#7E868E",0.2);
    [bgView2 addSubview:line_vertical];
    
    _totalUserLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(20) textColor:kCOLOR_HEX(@"#405DBE") textAligment:NSTextAlignmentCenter];
    [bgView2 addSubview:_totalUserLab];
    
    QMUILabel *totalTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentCenter];
    totalTitleLab.text = @"用户数量".rh_localized;
    [bgView2 addSubview:totalTitleLab];
    
    _groupTypeLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(20) textColor:kCOLOR_HEX(@"#405DBE") textAligment:NSTextAlignmentCenter];
    _groupTypeLab.adjustsFontSizeToFitWidth = YES;
    _groupTypeLab.minimumScaleFactor = 0.5;
    [bgView2 addSubview:_groupTypeLab];
    
    QMUILabel *normalTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentCenter];
    normalTitleLab.text = @"分组类型".rh_localized;
    [bgView2 addSubview:normalTitleLab];
    
    _descriteLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX_A(@"#171A1D", 0.8) textAligment:NSTextAlignmentLeft];
    [bgView addSubview:_descriteLab];
    
    
    
   
    
    QMUIButton *setBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [setBtn setImagePosition:QMUIButtonImagePositionLeft];
    [setBtn setImage:[UIImage imageNamed:@"vpp_set_chg"] forState:UIControlStateNormal];
    [setBtn setTitle:@"强制充电".rh_localized forState:UIControlStateNormal];
    [setBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [setBtn setTitleColor:kCOLOR_HEX(@"#171A1D") forState:UIControlStateNormal];
    [setBtn setSpacingBetweenImageAndTitle:5];
    [setBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [bgView addSubview:setBtn];
    
   
    
    QMUIButton *editBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [editBtn setImagePosition:QMUIButtonImagePositionLeft];
    [editBtn setImage:[UIImage imageNamed:@"vpp_set_dchg"] forState:UIControlStateNormal];
    [editBtn setTitle:@"强制放电".rh_localized forState:UIControlStateNormal];
    [editBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [editBtn setTitleColor:kCOLOR_HEX(@"#171A1D") forState:UIControlStateNormal];
    [editBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [editBtn setSpacingBetweenImageAndTitle:5];
    [bgView addSubview:editBtn];
    
  
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];

    [_plantGroupName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.height.mas_equalTo(25);
    }];
    
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.top.equalTo(weakSelf.plantGroupName.mas_bottom).offset(10);
        make.height.offset(1);
    }];
    
  
    
    [bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(line.mas_bottom).offset(15);
        make.height.offset(80);
    }];

    [line_vertical mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.mas_offset(15);
        make.bottom.mas_offset(-15);
        make.width.mas_offset(1);
    }];
    
    [_totalUserLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(5);
        make.right.equalTo(line_vertical.mas_left).offset(-5);
        make.bottom.equalTo(bgView2.mas_centerY).offset(-3);
    }];
    
    [totalTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.totalUserLab);
        make.top.equalTo(bgView2.mas_centerY).offset(3);
    }];
    
    [_groupTypeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(line_vertical.mas_right).offset(5);
        make.right.mas_offset(-5);
        make.bottom.equalTo(bgView2.mas_centerY).offset(-3);
    }];
    
    [normalTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.groupTypeLab);
        make.top.equalTo(bgView2.mas_centerY).offset(3);
    }];
    
    [_descriteLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(bgView2.mas_bottom).offset(15);
    }];
    
    [setBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.descriteLab.mas_bottom).offset(10);
        make.centerX.equalTo(bgView).offset(-(kUI_WINDOW_W-30)/4);
        make.height.offset(40);
        make.width.lessThanOrEqualTo(@(kUI_WINDOW_W/2-15));
        make.bottom.mas_offset(-10);
    }];
    
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(setBtn);
        make.centerX.equalTo(bgView).offset((kUI_WINDOW_W-30)/4);
        make.height.offset(40);
        make.width.lessThanOrEqualTo(@(kUI_WINDOW_W/2-15));
    }];
    
    [[setBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.chgBlock) {
            weakSelf.chgBlock(weakSelf.model);
        }
    }];
    
    [[editBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (weakSelf.dchgBlock) {
            weakSelf.dchgBlock(weakSelf.model);
        }
    }];
}



@end
